//
//  TLMallListBannerTableViewCell.m
//  WXReader
//
//  Created by huaya on 2025/1/13.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMallListBannerTableViewCell.h"
#import "YJBannerView.h"
#import "TL_BannerCollectionViewCell.h"
#import "TLShortStoryViewController.h"

@interface TLMallListBannerTableViewCell ()<YJBannerViewDataSource, YJBannerViewDelegate>

@property (nonatomic, weak) YJBannerView *bannerView;


@end
@implementation TLMallListBannerTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.backgroundColor = kWhiteColor;
    [self createSubviews];
    
//    [self.bannerView reloadData];
}
- (void)setBannerList:(NSArray<TLMallBannerModel *> *)bannerList{
    if (bannerList) {
        _bannerList = bannerList;
        [_bannerView reloadData];
    }
}

#pragma mark - UI
- (void)createSubviews {
    YJBannerView *bannerView = [YJBannerView bannerViewWithFrame:CGRectMake(0, kQuarterMargin, SCREEN_WIDTH, 145) dataSource:self delegate:self emptyImage:kEmptyHoldImage placeholderImage:kEmptyHoldImage selectorString:NSStringFromSelector(@selector(setImageWithURL:placeholder:))];
    self.bannerView = bannerView;
    bannerView.repeatCount = 9999;
    bannerView.pageControlAliment = PageControlAlimentCenter;
    bannerView.autoDuration = 5.0f;
    bannerView.pageControlStyle = PageControlCustom;
    bannerView.pageControlDotSize = CGSizeMake(10, 5);
    bannerView.pageControlBottomMargin = 15;
    UIImage *highlight_image = [UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(29, 6)];
    highlight_image = [highlight_image imageByRoundCornerRadius:3.0];
    bannerView.customPageControlHighlightImage = highlight_image;
    UIImage *normal_image = [UIImage imageWithColor:RGBA(255, 255, 255, 0.5) size:CGSizeMake(29, 6)];
    normal_image = [normal_image imageByRoundCornerRadius:3.0];
    bannerView.customPageControlNormalImage = normal_image;
    bannerView.backgroundColor = UIColor.clearColor;
    [self addSubview:bannerView];
}

- (NSArray *)bannerViewRegistCustomCellClass:(YJBannerView *)bannerView
{
    return @[[TL_BannerCollectionViewCell class]];
}

- (Class)bannerView:(YJBannerView *)bannerView reuseIdentifierForIndex:(NSInteger)index
{
    return [TL_BannerCollectionViewCell class];
}

- (UICollectionViewCell *)bannerView:(YJBannerView *)bannerView customCell:(UICollectionViewCell *)customCell index:(NSInteger)index
{
    TL_BannerCollectionViewCell *cell = (TL_BannerCollectionViewCell *)customCell;

    [cell setBannerWithUrl:self.bannerList[index].coverPic];
    
    return cell;
}

- (NSArray *)bannerViewImages:(YJBannerView *)bannerView {
    
    return self.bannerList;
}

- (void)bannerView:(YJBannerView *)bannerView didSelectItemAtIndex:(NSInteger)index {
    
    TLMallBannerModel *bannerModel = self.bannerList[index];
    
    NSURL *url = [NSURL URLWithString:bannerModel.url];
    NSLog(@"%@ - %@",url.path.lowercaseString,[TL_UtilsHelper mk_dictionaryWithUrlQuery:url.query]);
    
    [TL_JumpManager jumpActionWithType:[url.path stringByReplacingOccurrencesOfString:@" " withString:@""] content:@"" otherParameter:[TL_UtilsHelper mk_dictionaryWithUrlQuery:url.query] completion:nil];
}

@end
